import 'package:aichat_flutter_wallet/module/module_trade/utils/index.dart';
import 'package:aichat_flutter_wallet/module_common/provider/wallet_info_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../../module_common/widgets/button/bottom_button.dart';
import '../../../../module_common/widgets/dialog/bottom_sheet_widget.dart';

//添加自定义代币的确认弹窗
class CustomTokenConfirmSheet extends StatefulWidget {
  final Map<String, dynamic>? tokenItem;
  final void Function()? onConfirm;

  CustomTokenConfirmSheet({Key? key, required this.tokenItem, required this.onConfirm}) : super(key: key);

  @override
  _CustomTokenConfirmSheetState createState() => _CustomTokenConfirmSheetState();
}

class _CustomTokenConfirmSheetState extends State<CustomTokenConfirmSheet> {
  // Map<String, dynamic> get tokenInfo => widget.tokenItem!;
  Map<String, dynamic>? tokenInfo;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      tokenInfo = widget.tokenItem!;
    });
  }

  @override
  Widget build(BuildContext context) {
    print(tokenInfo);
    return BottomSheetWidget(
      title: '',
      expand: false,
      child: Container(
        padding: EdgeInsets.only(left: 20.w, right: 20.w),
        child: Column(
          children: [
            Row(
              children: [
                Image.asset(
                  'assets/images/status/warn.png',
                  width: 20.w,
                  height: 20.w,
                ),
                SizedBox(width: 7.w),
                Text("Custom Token",
                    style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold, color: "#333333".toColor()))
              ],
            ),
            SizedBox(height: 5),
            Text(
              'Please confirm that the added token contract address is correct. For fund security, ensure that the addition is accurate and assume the risk yourself.',
              style: TextStyle(fontSize: 12.sp, color: "#868391".toColor()),
            ),
            SizedBox(height: 15.w),
            Row(
              children: [
                Text(
                  'Token',
                  style: TextStyle(fontSize: 13.sp, color: "#868391".toColor()),
                ),
                SizedBox(width: 10.w),
                Expanded(
                    child: Text(
                  tokenInfo?['symbol'] ?? '',
                  textAlign: TextAlign.right,
                  style: TextStyle(fontSize: 13.sp, color: "#333333".toColor()),
                ))
              ],
            ),
            SizedBox(height: 15.w),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Contract address',
                  style: TextStyle(fontSize: 13.sp, color: "#868391".toColor()),
                ),
                SizedBox(width: 10.w),
                Expanded(
                    child: Text(
                  tokenInfo != null ? Utils.formatAddress(tokenInfo!['address']) : '',
                  textAlign: TextAlign.right,
                  style: TextStyle(fontSize: 13.sp, color: "#333333".toColor()),
                )),
                SizedBox(width: 10.w),
                GestureDetector(
                  onTap: () {
                    Chain c = context.read<WalletInfoNotifier>().walletChain!;
                    TranUtils.goBlockChainScan(context, c, contract: tokenInfo!['address']);
                  },
                  child: Image.asset(
                    'assets/images/wallet/token_browser.png',
                    width: 16.w,
                    height: 16.w,
                  ),
                )
              ],
            ),
            SizedBox(height: 20.w),
            BottomBtn(
              textList: ['Confirm'],
              onConfirm: () {
                //完成逻辑
                //1、检验、查询
                if (widget.onConfirm != null) {
                  widget.onConfirm!();
                  return;
                }
              },
            ),
            SizedBox(height: (MediaQuery.of(context).padding.bottom + 11).w)
          ],
        ),
      ),
    );
  }
}
